package cn.jn.lhm.netty.part06;

import io.netty.buffer.ByteBufAllocator;

import static cn.jn.lhm.util.ByteBufUtils.log;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/7/19 - 21:15
 **/
public class TestByteBufSlice {
    public static void main(String[] args) {
        var buffer = ByteBufAllocator.DEFAULT.buffer(10);
        buffer.writeBytes(new byte[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'});
        log(buffer);


        // 切片对 最大容量进行了限制，不能继续进行数据的写入
        // 不会发生数据的复制
        var buf1 = buffer.slice(0, 5);
        buf1.retain();
        var buf2 = buffer.slice(5, 5);
        buf2.retain();

        System.out.println("buf1.getClass() = " + buf1.getClass());
        System.out.println("buf2.getClass() = " + buf2.getClass());

        buf1.setByte(1, 'c');

        log(buf1);
        log(buf2);

        System.out.println(buffer + "--" + buf1 + "--" + buf2);

        buffer.release();
        System.out.println(buffer.refCnt() + "--" + buf1.refCnt() + "--" + buf2.refCnt());
        buf1.release();
        System.out.println(buffer.refCnt() + "--" + buf1.refCnt() + "--" + buf2.refCnt());
        buf2.release();
        System.out.println(buffer.refCnt() + "--" + buf1.refCnt() + "--" + buf2.refCnt());
    }

}
